<?php

namespace app\api\service;

use think\Db;

/**
 * 公共接口
 */
class SaleRank
{

    /**
     * 统计销售排行榜
     */
    public function saleRank(){
        $date = date('Y-m-d',strtotime('-1 day'));
        $lastMonthLastDay = date('Y-m-t', strtotime('last month'));
        $between = [strtotime(date('Y-m-01 00:00:00',strtotime('-1 day'))),strtotime(date('Y-m-d 23:59:59',strtotime('-1 day')))];
        // $between = [strtotime(date('Y-m-01 00:00:00')),strtotime('today 23:59:59')];
        // $date = date('Y-m-d');

        $list = \app\api\model\CommissionRecords::alias('commissionRecords')->field('commissionRecords.sale_id,sum(commission_price) as total_commission_price')
                                                ->join('user','user.id=commissionRecords.sale_id')
                                                ->where('commissionRecords.createtime','between',$between)
                                                ->group('sale_id')
                                                ->order('total_commission_price desc,user.createtime')
                                                ->select();

        $ids = array_column($list,'sale_id');
        $userWhere = [];
        if(count($ids) > 0){
            $userWhere['id'] = ['not in',$ids];
        }
        $userList = \app\api\model\User::field('id')->where($userWhere)->where('role',20)->order('createtime desc')->select();

        $saveList = [];
        $saveMonthList = [];
        $rank_discount = config('site.rank_discount') ?? [];

        foreach($list as $key=>$item){
            $discount = isset($rank_discount[$key+1]) && $rank_discount[$key+1] > 0 ? $rank_discount[$key+1] : 0;
            $data = [
                'type'=>20,
                'user_id'=>$item['sale_id'],
                'price'=>$item['total_commission_price'],
                'rank'=>$key+1,
                'discount'=>$discount,
                'date'=>$date,
                'count'=>\app\api\model\order\Order::where('sole_id',$item['sale_id'])->where('status',30)->where('songjiantime','between',$between)->count('id')
            ];
            $saveList[] = $data;
            $data['type'] = 10;
            $saveMonthList[] = $data;
        }
        
        foreach($userList as $key=>$item){
            $data = [
                'type'=>20,
                'user_id'=>$item['id'],
                'price'=>0,
                'rank'=>$key+count($ids) + 1,
                'discount'=>0,
                'date'=>$date,
                'count'=>0
            ];
            $saveList[] = $data;
            $data['type'] = 10;
            $saveMonthList[] = $data;
        }

        Db::name('sale_rank')->where('type',20)->where('date','between',[date('Y-m-d H:i:s',$between[0]),date('Y-m-d H:i:s',$between[1])])->delete();
        (new \app\api\model\SaleRank)->saveAll($saveList);
        if($lastMonthLastDay == $date){
            Db::name('sale_rank')->where('type',10)->where('date','between',[date('Y-m-d H:i:s',$between[0]),date('Y-m-d H:i:s',$between[1])])->delete();
            (new \app\api\model\SaleRank)->saveAll($saveMonthList);
        }

    }



}